<?php

namespace App\Services\MakeSeckillOrder;

use App\Models\User;
use Carbon\Carbon;
use App\Models\SeckillProduct;
use App\Http\Controllers\Api\V1\Requests\FormRequest;

class MakeSeckillOrderRequest extends FormRequest
{
	public function rules()
	{
		return [
			'sid' => [
				'required',
				'integer',
				'min:1',
				$this->seckillProduct(),
			],
			
		];
	}

	public function seckillProduct()
	{
		return function ($key, $value, $fail) {

			$sid = $this->sid;
			$time = Carbon::now();

			if (!SeckillProduct::sIsInCache($sid)) {
				return $fail('秒杀商品不存在');
			} elseif (!SeckillProduct::sIsInSoldCache($sid)) {
				return $fail('当前商品已抢光');
			} 

			return true;
		};
	}

}
